package action;

import config.ApplicationConfig;
import net.sf.json.JSONObject;
import server.*;
import util.Decode;
import util.Encode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.LinkedHashMap;

/**
 * User: tum0r
 * Package Name: ${PACKAGE_NAME}
 * Time: 2019-04-22 Monday 15:26:11
 */
public class SubmitSubjectServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LinkedHashMap<String, String> pageConfig = ApplicationConfig.readConfig("page.json");
        request.setCharacterEncoding(pageConfig.get("encode"));
        response.setCharacterEncoding(pageConfig.get("encode"));

        HttpSession session = request.getSession();
        String username = session.getAttribute("user") == null ? "" : session.getAttribute("user").toString();
        String cookie = session.getAttribute("cookie") == null ? "" : session.getAttribute("cookie").toString();
        String page = "information.jsp";
        if (username == null || username.equals("") || cookie == null || cookie.equals("")) {
            session.setAttribute("infoTitle", "未登录");
            session.setAttribute("infoContent", "您还未登录，请先登录");
            session.setAttribute("return", "index.jsp");
            session.setAttribute("panelColor", "red");
            session.removeAttribute("user");
            session.removeAttribute("cookie");
        } else {
            LoginServer loginServer = new LoginServer();
            loginServer.open();
            if (!loginServer.checkLogin(username, cookie)) {
                loginServer.updateLoginTime(username);
                GameServer gameServer = new GameServer();
                gameServer.open();
                JSONObject gameInformation = gameServer.getGameInformation();
                gameServer.close();
                long startTime = gameInformation.getLong("startTime");
                long stopTime = gameInformation.getLong("stopTime");
                long now = new Date().getTime();
                if (now >= startTime && now <= stopTime) {
                    String subjectID = request.getParameter("subjectID");
                    String code = request.getParameter("code");
                    String language = request.getParameter("language");
                    if (subjectID != null && code != null && language != null) {
                        Decode decode = new Decode();
                        subjectID = decode.urlUtf8Decode(subjectID);
                        language = decode.urlUtf8Decode(language);
                        RecordServer recordServer = new RecordServer();
                        recordServer.open();
                        if (recordServer.recordSubject(subjectID, username, code, language)) {
                            UserServer userServer = new UserServer();
                            userServer.open();
                            userServer.setUserLastLanguage(username, language);
                            userServer.close();
                            Encode encode = new Encode();
                            page = "details.jsp?user=" + encode.urlEncode(username);
                        } else {
                            session.setAttribute("infoTitle", "提交代码");
                            session.setAttribute("infoContent", "提交失败，账户可能已被封禁");
                            session.setAttribute("return", "index.jsp");
                            session.setAttribute("panelColor", "red");
                        }
                        recordServer.close();
                    }
                } else if (now > stopTime) {
                    session.setAttribute("infoTitle", "提交代码");
                    session.setAttribute("infoContent", "提交失败，比赛已结束");
                    session.setAttribute("return", "index.jsp");
                    session.setAttribute("panelColor", "red");
                } else if (now < stopTime) {
                    session.setAttribute("infoTitle", "提交代码");
                    session.setAttribute("infoContent", "提交失败，比赛暂未开始");
                    session.setAttribute("return", "index.jsp");
                    session.setAttribute("panelColor", "red");
                }
            } else {
                session.setAttribute("infoTitle", "未登录");
                session.setAttribute("infoContent", "您还未登录，请先登录");
                session.setAttribute("return", "index.jsp");
                session.setAttribute("panelColor", "red");
                session.removeAttribute("user");
                session.removeAttribute("cookie");
            }
            loginServer.close();
        }
        response.sendRedirect(page);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
